﻿using System;
using System.Runtime.InteropServices;

namespace APP.HELPER.Utils.CustomizeToolbox
{
    public class CSysMsgBoxHook
    {
        private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        private const int WH_CBT = 5;
        private const long HCBT_ACTIVATE = 5L;
        private const int ID_BUT_OK = 1;
        private const int ID_BUT_CANCEL = 2;
        private const int ID_BUT_ABORT = 3;
        private const int ID_BUT_RETRY = 4;
        private const int ID_BUT_IGNORE = 5;
        private const int ID_BUT_YES = 6;
        private const int ID_BUT_NO = 7;
        private const string BUT_OK = "Ðồng ý";
        private const string BUT_CANCLE = "Thôi";
        private const string BUT_ABORT = "Huỷ";
        private const string BUT_RETRY = "Thử lại";
        private const string BUT_IGNORE = "Bỏ qua";
        private const string BUT_YES = "Có";
        private const string BUT_NO = "Không";
        private IntPtr _hook;
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        private static extern IntPtr SetWindowsHookEx(int idHook, CSysMsgBoxHook.HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(IntPtr idHook);
        [DllImport("user32.dll")]
        private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        private static extern bool SetDlgItemTextW(IntPtr hWnd, int nIDDlgItem, IntPtr lpString);
        [DllImport("gdi32.dll", CharSet = CharSet.Ansi, EntryPoint = "CreateFontA", ExactSpelling = true, SetLastError = true)]
        public static extern int CreateFont(int H, int W, int E, int O, int w, int I, int u, int S, int C, int OP, int CP, int Q, int PAF, string F);
        [DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "SendMessageA", ExactSpelling = true, SetLastError = true)]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "SetWindowTextW", ExactSpelling = true, SetLastError = true)]
        public static extern int SetWindowText(IntPtr hwnd, IntPtr lpString);
        [DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "FindWindowExA", ExactSpelling = true, SetLastError = true)]
        public static extern int FindWindowEx(IntPtr hWnd1, int hWnd2, string lpsz1, string lpsz2);
        public void HookDialog()
        {
            this.UnHookDialog();
            this._hook = CSysMsgBoxHook.SetWindowsHookEx(5, new CSysMsgBoxHook.HookProc(this.DialogHookProc), IntPtr.Zero, AppDomain.GetCurrentThreadId());
        }
        public void UnHookDialog()
        {
            if (!this._hook.Equals(IntPtr.Zero))
            {
                CSysMsgBoxHook.UnhookWindowsHookEx(this._hook);
            }
        }
        private IntPtr DialogHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            IntPtr result;
            if (nCode < 0)
            {
                result = CSysMsgBoxHook.CallNextHookEx(this._hook, nCode, wParam, lParam);
            }
            else
            {
                if ((long)nCode == 5L)
                {
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 1, Marshal.StringToHGlobalUni("Ðồng ý"));
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 2, Marshal.StringToHGlobalUni("Thôi"));
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 3, Marshal.StringToHGlobalUni("Huỷ"));
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 4, Marshal.StringToHGlobalUni("Thử lại"));
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 5, Marshal.StringToHGlobalUni("Bỏ qua"));
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 6, Marshal.StringToHGlobalUni("Có"));
                    CSysMsgBoxHook.SetDlgItemTextW(wParam, 7, Marshal.StringToHGlobalUni("Không"));
                    CSysMsgBoxHook.UnhookWindowsHookEx(this._hook);
                }
                result = CSysMsgBoxHook.CallNextHookEx(this._hook, nCode, wParam, lParam);
            }
            return result;
        }
    }
}
